package com.label.Service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.label.Model.Do.User;
import com.label.Service.LabelService;
import com.label.Utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class LabelServiceImpl implements LabelService {

    @Autowired
    @Qualifier("oracleJdbcTemplate")
    private JdbcTemplate oraclejdbc;

    @Override
    @Cacheable(value = "label",key = "'id'+#id+'_str'+#str")
    public List<Map<String, Object>> getLike(Integer id, String str) {
        User user= UserUtil.getUser();
        StringBuffer sql=new StringBuffer();
        sql.append("select value,label from label where label_id=").append(id).append(" and label like '%").append(str).append("%'");
        if(id<=9){
            return oraclejdbc.queryForList(String.valueOf(sql));
        }else if(id<=12){
            switch (user.getOpLevel()){
                case "0":return oraclejdbc.queryForList(String.valueOf(sql));
                case "1":return oraclejdbc.queryForList(String.valueOf(sql.append(" and school_id=").append(user.getSchoolId())));
                case "2":return oraclejdbc.queryForList(String.valueOf(sql.append(" and school_id=").append(user.getSchoolId()).append(" and unit_id=").append(user.getUnitId())));
                default: return null;
            }
        }else{
            return oraclejdbc.queryForList(String.valueOf(sql.append(" and school_id=").append(user.getSchoolId())));
        }
    }

    @Override
    @Cacheable(value = "label",key = "'getlabel'")
    public String getLabel() {
        List<Map<String, Object>> profileList = new ArrayList<Map<String, Object>>();
        List<Map<String,Object>> labels=new ArrayList<>();
        //属性map
        labels=oraclejdbc.queryForList("select * from profile_temp where type='1'");
        Map<String,Object>  amap=new HashMap<String, Object>();
        amap.put("name","用户属性");
        amap.put("type","attr");
        amap.put("children",getMap(labels));
        profileList.add(amap);

        //事件map
        Map<String,Object>  emap=new HashMap<String, Object>();
        emap.put("name","事件标签");
        emap.put("type","event");
        emap.put("children",getEventMap());
        profileList.add(emap);

        return JSONObject.toJSONString(profileList);
    }

    public static Map<String,Integer> profile=ImmutableMap.of("studentprofile",1,"dormitoryprofile",2,"classprofile",3,"majorprofile",4);

    @Override
    @Cacheable(value = "label",key = "'getCardLabel'+#theme")
    public String getCardLabel(String theme){
        switch (profile.get(theme)){
            case 1:return getStudentLable();
            case 2:return getDormitoryLable();
            case 3:return getClassLable();
            case 4:return getMajorLable();
            default:return "";
        }
    }

    private String getCourseLable() {
        List<Map<String,Object>> labels=oraclejdbc.queryForList("select * from profile_temp where id in (11,14)");
        return JSONObject.toJSONString(getMap(labels));
    }

    private String getUnitLable() {
        return "";
    }

    private String getMajorLable() {
        List<Map<String,Object>> labels=oraclejdbc.queryForList("select * from profile_temp where id in (10,11)");
        return JSONObject.toJSONString(getMap(labels));
    }

    private String getDormitoryLable() {
        List<Map<String,Object>> labels=oraclejdbc.queryForList("select * from profile_temp where id in (21,22,23,24)");
        return JSONObject.toJSONString(getMap(labels));
    }

    private String getClassLable() {
        List<Map<String,Object>> labels=oraclejdbc.queryForList("select * from profile_temp where id in (10,11,13)");
        return JSONObject.toJSONString(getMap(labels));
    }

    private String getStudentLable() {
        List<Map<String,Object>> labels=oraclejdbc.queryForList("select * from profile_temp where type='1'");
        return JSONObject.toJSONString(getMap(labels));
    }


    private List<Map<String,Object>> getMap(List<Map<String,Object>> table){
        List<Map<String,Object>> attrRes=new ArrayList<>();
        for(Map<String,Object> row:table){
            attrRes.add(ImmutableMap.of("table",row.get("LABEL_TYPE")==null?"":row.get("LABEL_TYPE").toString(),"value",row.get("VALUE"),"operation",getOperation(Integer.parseInt(row.get("OPERATION").toString())),"name",row.get("NAME")));
        }
        return attrRes;
    }

    private List<Map<String, Object>> getEventMap(){
        List<Map<String,Object>> eventMap=new ArrayList<>();
        List<Map<String,Object>> labels=new ArrayList<>();

        labels=oraclejdbc.queryForList("select * from profile_temp where type='2'");
        Map<String,Object>  course=new HashMap<>();

        course.put("children",getMap(labels));
        course.put("name", "选课事件");
        course.put("value","studentcourse");
        Map<String,Object>  e1_1 = ImmutableMap.of("VALUE","courseid","LABLE","总科目数","operation",getOperation(2));
        Map<String,Object>  e1_2 = ImmutableMap.of("VALUE","credit","LABLE","总学分","operation",getOperation(2));
        List<Map<String,Object>> e1= Arrays.asList(e1_1,e1_2);
        course.put("source",e1);

        eventMap.add(course);


        //考试表
        labels=oraclejdbc.queryForList("select * from profile_temp where type='3'");
        Map<String,Object>  exam=new HashMap<>();

        exam.put("children",getMap(labels));
        exam.put("name", "考试事件");
        exam.put("value","studentexam");
        Map<String,Object>  e2_1 = ImmutableMap.of("VALUE","courseid","LABLE","总科目数","operation",getOperation(2));
        Map<String,Object>  e2_2 = ImmutableMap.of("VALUE","score","LABLE","总成绩","operation",getOperation(2));
        Map<String,Object>  e2_3 = ImmutableMap.of("VALUE","scoregpa","LABLE","总绩点成绩","operation",getOperation(2));
        Map<String,Object>  e2_4 = ImmutableMap.of("VALUE","credit","LABLE","总学分","operation",getOperation(2));
        List<Map<String,Object>> e2=Arrays.asList(e2_1,e2_2,e2_3,e2_4);
        exam.put("source",e2);

        eventMap.add(exam);

        //活动事件
        labels=oraclejdbc.queryForList("select * from profile_temp where type='4'");
        Map<String,Object>  activity=new HashMap<>();

        activity.put("children",getMap(labels));
        activity.put("value","studentactivity");
        activity.put("name", "活动事件");
        Map<String,Object>  e3_1 = ImmutableMap.of("VALUE","activityid","LABLE","总次数","operation",getOperation(2));
        Map<String,Object>  e3_2 = ImmutableMap.of("VALUE","rewardresult","LABLE","总学分","operation",getOperation(2));
        Map<String,Object>  e3_3 = ImmutableMap.of("VALUE","virtue","LABLE","总德育学分","operation",getOperation(2));
        Map<String,Object>  e3_4 = ImmutableMap.of("VALUE","wisdom","LABLE","总智育学分","operation",getOperation(2));
        Map<String,Object>  e3_5 = ImmutableMap.of("VALUE","sports","LABLE","总体育学分","operation",getOperation(2));
        Map<String,Object>  e3_6 = ImmutableMap.of("VALUE","art","LABLE","总美育学分","operation",getOperation(2));
        Map<String,Object>  e3_7 = ImmutableMap.of("VALUE","labour","LABLE","总劳育学分","operation",getOperation(2));
        List<Map<String,Object>> e3=Arrays.asList(e3_1,e3_2,e3_3,e3_4,e3_5,e3_6,e3_7);
        activity.put("source",e3);

        eventMap.add(activity);

        return eventMap;
    }

    public List<Map<String,String>> getOperation(int type){
        if(type==1)
        {
            Map<String,String>  ltype1 = ImmutableMap.of("operation","是","operationValue","equal");
            Map<String,String>  ltype2= ImmutableMap.of("operation","否","operationValue","notequal");
            List<Map<String,String>> tf=Arrays.asList(ltype1,ltype2);
            return tf;
        }
        else if(type==2){
            Map<String,String>  ltype1 = ImmutableMap.of("operation","大于","operationValue",">");
            Map<String,String>  ltype2= ImmutableMap.of("operation","小于","operationValue","<");
            Map<String,String>  ltype3 = ImmutableMap.of("operation","等于","operationValue","=");
            Map<String,String>  ltype4= ImmutableMap.of("operation","不等于","operationValue","!=");
            Map<String,String>  ltype5 = ImmutableMap.of("operation","大于等于","operationValue",">=");
            Map<String,String>  ltype6= ImmutableMap.of("operation","小于等于","operationValue","<=");
            List<Map<String,String>> tf=Arrays.asList(ltype1,ltype2,ltype3,ltype4,ltype5,ltype6);
            return tf;
        }
        else if(type==3) {
            Map<String,String>  ltype1 = ImmutableMap.of("operation","是","operationValue","true");
            Map<String,String>  ltype2= ImmutableMap.of("operation","否","operationValue","false");
            List<Map<String,String>> tf=Arrays.asList(ltype1,ltype2);
            return tf;
        }
        else {
            Map<String,String>  ltype1 = ImmutableMap.of("operation","是","operationValue","like");
            Map<String,String>  ltype2= ImmutableMap.of("operation","否","operationValue","notlike");
            List<Map<String,String>> tf=Arrays.asList(ltype1,ltype2);
            return tf;
        }
    }


}
